package com.videostreaming.db;

import com.gooddies.wiring.annotations.PostWiring;
import com.gooddies.wiring.annotations.WiringComponent;
import com.videostreaming.data.User;
import java.util.List;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * @author sad
 */
@WiringComponent(singleton = true)
public class DBImpl {

    private SqlSessionFactory sqlSessionFactory;

    private DBImpl() {
    }
    
    public SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }

    @PostWiring
    private void init() {
        DataSource dataSource = new UnpooledDataSource("org.sqlite.JDBC", "jdbc:sqlite:D:/videoservice.sqlite", null, null);
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(DB.class);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    }
}
